package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoUserFollows;
import com.muyu.danmo.service.IDanmoUserFollowsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 关注关系Controller
 *
 * @author yangle
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/userFollows")
public class DanmoUserFollowsController extends BaseController {
    @Autowired
    private IDanmoUserFollowsService danmoUserFollowsService;

    /**
     * 查询关注关系列表
     */
//    @RequiresPermissions("system:userFollows:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoUserFollows danmoUserFollows) {
        PageHelper.startPage(danmoUserFollows.getPageNum(), danmoUserFollows.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFollowerId(loginUser.getUserid());
        List<DanmoUserFollows> list = danmoUserFollowsService.selectDanmoUserFollowsList(danmoUserFollows);
        if (list.size() == 0){
            return Result.error("暂无关注");
        }
        PageInfo<DanmoUserFollows> danmoUserFollowsPageInfo = new PageInfo<>(list);
        return Result.success(danmoUserFollowsPageInfo);
    }

    /**
     * 导出关注关系列表
     */
//    @RequiresPermissions("system:userFollows:export")
    @Log(title = "关注关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoUserFollows danmoUserFollows) {
        List<DanmoUserFollows> list = danmoUserFollowsService.selectDanmoUserFollowsList(danmoUserFollows);
        ExcelUtil<DanmoUserFollows> util = new ExcelUtil<DanmoUserFollows>(DanmoUserFollows.class);
        util.exportExcel(response, list, "关注关系数据");
    }

    /**
     * 获取关注关系详细信息
     */
//    @RequiresPermissions("system:userFollows:query")
    @GetMapping(value = "/{followId}")
    public Result getInfo(@PathVariable("followId") Long followId) {
        return Result.success(danmoUserFollowsService.selectDanmoUserFollowsByFollowId(followId));
    }

    /**
     * 新增关注
     */
//    @RequiresPermissions("system:userFollows:add")
    @Log(title = "关注关系", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public Result add(@RequestBody DanmoUserFollows danmoUserFollows) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFollowerId(loginUser.getUserid());
        if (danmoUserFollows.getFolloweeId().equals(danmoUserFollows.getFollowerId())) {
            return Result.error("不能关注自己");
        }
        return danmoUserFollowsService.insertDanmoUserFollows(danmoUserFollows);
    }

    /**
     * 修改关注关系
     */
//    @RequiresPermissions("system:userFollows:edit")
    @Log(title = "关注关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoUserFollows danmoUserFollows) {
        return danmoUserFollowsService.updateDanmoUserFollows(danmoUserFollows);
    }

    /**
     * 删除关注关系
     */
//    @RequiresPermissions("system:userFollows:remove")
/*    @Log(title = "关注关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{followIds}")
    public Result remove(@PathVariable Long[] followIds) {
        return danmoUserFollowsService.deleteDanmoUserFollowsByFollowIds(followIds);
    }*/


    /**
     * 取消关注
     */
//    @Log(title = "关注关系", businessType = BusinessType.DELETE)
    @GetMapping("delete/{followeeId}")
    public Result remove(@PathVariable Long followeeId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        DanmoUserFollows danmoUserFollows = new DanmoUserFollows();
        danmoUserFollows.setFolloweeId(followeeId);
        danmoUserFollows.setFollowerId(loginUser.getUserid());
        return danmoUserFollowsService.remove(danmoUserFollows);
    }

    /**
     * 查询粉丝列表
     */
    @PostMapping("/fanList")
    public Result fanList(@RequestBody DanmoUserFollows danmoUserFollows) {
        PageHelper.startPage(danmoUserFollows.getPageNum(), danmoUserFollows.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFolloweeId(loginUser.getUserid());
        List<DanmoUserFollows> list = danmoUserFollowsService.selectDanmoUserFollowsfanList(danmoUserFollows);
        if (list.size() == 0) {
            return Result.error("暂无粉丝");
        }
        PageInfo<DanmoUserFollows> danmoUserFollowsPageInfo = new PageInfo<>(list);
        return Result.success(danmoUserFollowsPageInfo);
    }

    /**
     * 查询粉丝数量
     */
    @GetMapping("/count")
    public Result count(@RequestBody DanmoUserFollows danmoUserFollows) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFolloweeId(loginUser.getUserid());
        List<DanmoUserFollows> list = danmoUserFollowsService.selectDanmoUserFollowsfanList(danmoUserFollows);
        return Result.success(list.size());
    }

    /**
     * 查询关注数量
     */
    @GetMapping("/attentionCount")
    public Result attentionCount(@RequestBody DanmoUserFollows danmoUserFollows) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFollowerId(loginUser.getUserid());
        List<DanmoUserFollows> list = danmoUserFollowsService.selectDanmoUserFollowsfanList(danmoUserFollows);
        return Result.success(list.size());
    }


    /**
     * 查询自己的关注
     */
    @PostMapping("/myFollowList")
    public Result myFollowList(@RequestBody DanmoUserFollows danmoUserFollows) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoUserFollows.setFollowerId(loginUser.getUserid());
        List<DanmoUserFollows> list = danmoUserFollowsService.selectUserList(danmoUserFollows);
       if (list.size() == 0) {
           return Result.error("暂无关注");
       }
       PageInfo<DanmoUserFollows> danmoUserFollowsPageInfo = new PageInfo<>(list);
       return Result.success(danmoUserFollowsPageInfo);
    }

}
